package com.ligongmusic.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
/**
 * 自定义ListView的适配器
 * @author wly
 * @param <T>
 */
public abstract class UniversalAdapter<T> extends BaseAdapter {

	protected Context mContext;
	protected List<T> mDatas;
	private int layoutID = -1;
	private View layout;
	/**
	 * @param mContext 上下文
	 * @param mDatas 数据集合
	 * @param layoutID 布局ID
	 */
	public UniversalAdapter(Context mContext, List<T> mDatas, int layoutID) {
		super();
		this.mContext = mContext;
		this.mDatas = mDatas;
		this.layoutID = layoutID;
	}
	
	
	/**
	 * @param mContext  上下文
	 * @param mDatas  数据
	 * @param layout  加载完成后的布局View
	 */
	public UniversalAdapter(Context mContext, List<T> mDatas, View layout) {
		super();
		this.mContext = mContext;
		this.mDatas = mDatas;
		this.layout = layout;
	}

	@Override
	public int getCount() {
		return mDatas.size();
	}

	@Override
	public T getItem(int position) {
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if(layoutID == -1){
			holder = ViewHolder.getViewHolder(mContext, parent, convertView, layout, position);
		} else {
			holder = ViewHolder.getViewHolder(mContext, parent, convertView, layoutID, position);
		}
		convert(holder, getItem(position), position);
		return holder.getConvertView();
	}
	
	/**
	 * getView()方法中的操作，用户自己实现
	 * @param holder
	 * @param t
	 */
	public abstract void convert(ViewHolder holder,T t, int position);

	
	public void refresh(List<T> data){
		this.mDatas = data;
		notifyDataSetChanged();
	}
}
